package practice;

// 编写代码, 基于 AtomicInteger 实现多线程自增同一个变量

import java.util.concurrent.atomic.AtomicInteger;

public class Main3 {
    public static void main(String[] args) throws InterruptedException {
        AtomicInteger count = new AtomicInteger();

        Thread t1 = new Thread(() -> {
            count.getAndDecrement();
            System.out.println("t1进行后置--");
        });

        Thread t2 = new Thread(() -> {
            count.incrementAndGet();
            System.out.println("t2进行前置++");
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });

        Thread t3 = new Thread(() -> {
            System.out.println("t3开始循环后置++");
            for (int i = 0; i < 8; i++) {
                count.getAndIncrement();
            }
        });

        t1.start();
        t2.start();
        t3.start();

        // 一定要阻塞等到
        t1.join();
        t2.join();
        t3.join();

        System.out.println(count);
    }
}
